﻿/*
后台管理页面 工具类 <br/>
前置条件: jquery.js, underscore.js <br/>
@author gonglong fsl 20151121
 */
var adminUtil = {};

adminUtil.templates = {
    selectTempl: "<option value=''>请选择</option>" +
        "<% _.each(obj, function(item){ %> <option value='<%=item.code %>'><%=item.text %></option> <% }); %>",
};

/*
翻页插件。
@param domSelector : jq 选择器;
@param pageConfig : 配置参数对象，
pageConfig = {
perpage : "每页记录数",
page : "当前页码",
total : "总记录数",
onSelect : 回调函数,翻页事件.
}
 */
adminUtil.initPaging = function(domSelector, pageConfig) {
    var conf = {
        pageSize: 0,
        page: 0,
        total: 0,
        onClick: function(page) {}
    };
    conf = $.extend({}, conf, pageConfig);

    var pagingObj = $(domSelector || ".comm-paging").empty();

    var paging = pagingObj.paging(conf.total, {
        format: "[< nncnn >]",
        perpage: conf.pageSize,
        page: conf.page,
        onClick: conf.onClick,
        onFormat: function(type) {
            switch (type) {
                case 'block': // n and c
                    if (!this.active) {
                        return "<a class='disable'>" + this.value + "</a>";
                    } else if (this.value == this.page) {
                        pagingObj.attr("current-page", this.value);
                        return "<a class='current'>" + this.value + "</a>";
                    } else {
                        return "<a>" + this.value + "</a>";
                    }

                    return '<a>' + this.value + '</a>';

                case 'next': // >

                    if (this.page == this.pages || this.pages == 0) {
                        return "<a  class='disable'>下一页</a>";
                    }
                    return '<a>下一页</a>';
                case 'prev': // <
                    if (this.page == 1) {
                        return "<a  class='disable'>上一页</a>";
                    }
                    return '<a>上一页</a>';
                case 'first': // [
                    if (this.page == 1) {
                        return "<a  class='disable'>首页</a>";
                    }
                    return '<a>首页</a>';
                case 'last': // ]
                    if (this.page == this.pages || this.pages == 0) {
                        return "<a  class='disable'>尾页</a>";
                    }
                    return '<a>尾页</a>';
            }
            return "";
        }
    });

    return paging;
};

/**
 * 纯提示信息-比如: 错误消息 提示框.
 * @param conf: {title : "注意",message : "获取公司资料失败!",ok_txt : "确定"}
 * @author fsl-gongl.
 */
adminUtil.showMsg = function(conf) {

    var templHtml = "<div class='dialog-div'><div style='font-size: 15px;'><%= message %></div>" +
        "<div class='price-game-btn'><button type='button' class='am-btn am-btn-primary am-btn-xs' id='ok-btn' code='' style='margin: 0 auto; display:block;margin-top: 10px;'>" +
        "<%= ok_txt %></button></div></div>";

    var def = {
        title: "注意",
        message: "请确认你的操作!",
        ok_txt: "确定"
    };

    var data = $.extend(def, conf);

    var tmplObj = _.template(templHtml);
    var tmplObjHtml = tmplObj(data);

    var dia = dialog({
        id: "comm-error-box",
        title: data.title,
        content: tmplObjHtml,
        onshow: function() {
            var that = this;
            $("[id='content:comm-error-box']").find("#ok-btn").on("click", function() {
                that.remove();
            });
        }
    });
    dia.showModal();

};


adminUtil.showConfDialog = function(conf) {

    var tmplHtmlStr = "<div class='main-box'>" +
        "<div class='msg-box'><%= message %></div>" +
        "<div class='bottom-btns'>" +
        "<div style='width:155px; margin:0 auto'><button class='am-btn am-btn-secondary sel-ok-btn' style=''>确认</button>" +
        "<button class='am-btn am-btn-default sel-cancel-btn' style='margin-left: 20px;'>取消</button></div>" +
        "</div> </div>";
    var tmplObj = _.template(tmplHtmlStr);

    var defConf = {
        tmplHtml: "",
        title: "注意",
        message: "请确认你的操作!",
        ok_txt: "确定",
        width: 380,
        height: 150,
        ok_func: null,
        ok_context: null,
        ok_param: null
    };

    var confData = $.extend(defConf, conf);

    confData.tmplHtml = tmplObj({
        message: confData.message
    });

    if (confData.tmplHtml == "") {
        return false;
    }

    var dia = dialog({
        id: "comm-dialog",
        title: confData.title,
        content: confData.tmplHtml,
        width: confData.width,
        height: confData.height,
        onshow: function() {
            var that = this;
            // 绑定"取消"按钮
            $("[id='content:comm-dialog']").find(".sel-cancel-btn").on("click", function() {
                that.remove();
            });

            // 绑定"确定"按钮
            $("[id='content:comm-dialog']").find(".sel-ok-btn").on("click", function() {
                if (!!confData.ok_func) {
                    confData.ok_func.call(confData.ok_context, confData.ok_param)
                }
                that.remove();
            });

        }
    });
    dia.showModal();
};

/**
 * showCommDialog
 *
 */
adminUtil.showCommDialog = function(conf) {

    var defConf = {
        tmplObj: null,
        tmplData: null,
        tmplHtml: "",
        title: "注意",
        message: "请确认你的操作!",
        ok_txt: "确定",
        width: 380,
        height: 150,
        ok_func: null,
        ok_context: null,
        ok_param: null
    };

    var confData = $.extend(defConf, conf);

    confData.tmplHtml = confData.tmplObj({
        message: confData.message
    });

    if (confData.tmplHtml == "") {
        return false;
    }

    var dia = dialog({
        id: "comm-dialog",
        title: confData.title,
        content: confData.tmplHtml,
        width: confData.width,
        height: confData.height,
        onshow: function() {
            var that = this;
            // 绑定"取消"按钮
            $("[id='content:comm-dialog']").find(".sel-cancel-btn").on("click", function() {
                that.remove();
            });

            // 绑定"确定"按钮
            $("[id='content:comm-dialog']").find(".sel-ok-btn").on("click", function() {
                if (!!confData.ok_func) {
                    confData.ok_func.call(confData.ok_context, confData.ok_param)
                }
                that.remove();
            });

        }
    });
    dia.showModal();

};

/**
 * 获取下拉列表。
 *
 * @param domSelector {string} 选择器, 如: "#id" OR ".class-css"
 * @param data {array} 填充模板数据.
 * 
 * @author fsl-gonglong 20151125
 */
adminUtil.getSelectHtml = function(domSelector, data) {

    // {code : "1000", text : "分类1"}
    var templ = adminUtil.templates.selectTempl;
    var templObj = _.template(templ);
    var tempHtml = templObj(data);
    $(domSelector).html(tempHtml);
};


/**
 * 比较时间大小<br/>
 * @param date1 {date} 比如: 11/23/2015
 * @param date2 {date} 比如: 11/25/2015
 * @return {boolean}
 *          true: date1 小于(早于) date2;
 *          false: date1 大于(晚于) date2;
 *
 * @author fsl-gongl20151103
 */
adminUtil.checkDate = function(date1, date2) {

    var startDate = date1.replace(/-/g, "/");
    var endDate = date2.replace(/-/g, "/");

    var start = new Date(startDate);
    var end = new Date(endDate);

    if (start <= end) {
        return true;
    }
    return false;

};


